library(ggplot2)
library(data.table)
library(scales)
library(readxl)
library(dplyr)
library(tidyverse)
library(showtext)
library(cowplot)
library(Cairo)

pth_reg <- "/System/Library/Fonts/Palatino.ttc"
font_add(family = "Palatino", regular = pth_reg)

setwd("/Users/jbhoward/Documents/Personal Research/JAERE Extension/FINAL SIMULATION")

out_pth <- file.path("figures", "output")

fp <- file.path("simulation", "counterfactual.xlsx")
dt <- read_excel(fp)

keep_taus <- c(1, 1.15)
dt$tau <- as.numeric(dt$tau)
dt <- dt %>% filter(tau %in% keep_taus)
dt$tariff <- "Frictionless Trade"
dt$tariff[dt$tau > 1] <- paste0((dt$tau[dt$tau > 1]-1) * 100, "% Trade Cost")

dt$`Simulation Key`[dt$`Simulation Key` == "IO"] <- "Random"

dt$inland_X <- as.character(as.numeric(dt$inland_etotal)/as.numeric(dt$inland_eint))
dt$coastal_X <- as.character(as.numeric(dt$coastal_etotal)/as.numeric(dt$coastal_eint))


lng <- dt %>% pivot_longer(!c("Simulation Key", "tau","mean_matchingprob", "tariff"))

lng$loc <- NA
lng$loc[grepl("inland", lng$name)] <- "Periphery"
lng$loc[grepl("coastal", lng$name)] <- "Core"

lng$var <- lng$name
lng$var <- gsub("coastal", "", lng$var)
lng$var <- gsub("inland", "", lng$var)
lng$var <- gsub("_", " ", lng$var)
lng$var <- gsub("  ", " ", lng$var)
lng$var <- trimws(lng$var, which = "both")


p <- lng %>% filter(var == "eint") %>%
  mutate(scalar = lng$value[lng$`Simulation Key` == "Endog" &
         lng$tau == 1 & lng$var == var & lng$loc == "Core"]) %>%
  mutate(value = as.numeric(value)/as.numeric(scalar)) %>%
  ggplot(aes(x=factor(`Simulation Key`),
            y = as.numeric(value),
            fill=paste0(`Simulation Key`, loc), linetype=`Simulation Key`, group = `loc`)) +
    geom_col(position = "dodge", width = 0.75, linewidth = 1, color = 'darkgrey') +
    scale_fill_brewer(palette="Paired") +
    geom_text(aes(label = `loc`, group = `loc`),
              vjust = -0.5, colour = "black", size = 3.25,
              position = position_dodge(width = 0.75)) +#,
              # family = "Palatino Linotype") +
    scale_linetype_manual(values=c("blank", "dashed")) +
    scale_y_continuous(breaks = c(1.0, 2.0, 3.0), limits = c(0, 3), oob = rescale_none) + 
    theme_minimal() + 
    labs(fill = NULL, x = NULL, y = "Emission Intensity", linetype = NULL) + 
    theme(legend.position = "none",  text = element_text(size = 12, colour = "black"), #, family = "serif"),
      axis.text = element_text(color = "black"),
      panel.spacing = unit(2, "lines"), legend.key.width= unit(1, 'cm')) +
    facet_grid(~ tariff)

showtext_auto()
fn <- "fig10b_cf_eint.eps"

cairo_ps(file.path(out_pth, fn), width = 8, height = 3, fallback_resolution = 320)
print(p)
dev.off()

showtext_auto(FALSE)


p <- lng %>% filter(var == "etotal") %>% 
  mutate(scalar = lng$value[lng$`Simulation Key` == "Endog" &
         lng$tau == 1 & lng$var == var & lng$loc == "Core"]) %>%
  mutate(value = as.numeric(value)/as.numeric(scalar)) %>%
  ggplot(aes(x=factor(`Simulation Key`),
            y = value,
            fill=paste0(`Simulation Key`, loc), linetype=`Simulation Key`, group = `loc`)) +
    geom_col(position = "dodge", width = 0.75, linewidth = 1, color = 'darkgrey') +
    scale_fill_brewer(palette="Paired", direction=1) +
    geom_text(aes(label = `loc`, group = `loc`),
              vjust = -0.5, colour = "black", size = 3.25,
              position = position_dodge(width = 0.75), family = 'Palatino') + #,
    scale_linetype_manual(values=c("blank", "dashed")) +
    scale_y_continuous(breaks = c(0.25,0.5,0.75, 1), limits = c(0, 1.3)) + 
    theme_minimal() + 
    labs(fill = NULL, x = NULL, y = "Emissions", linetype = NULL) + 
    theme(legend.position = "none",  text = element_text(size = 12, colour = "black", family = 'Palatino'),
      axis.text = element_text(color = "black"),
      panel.spacing = unit(2, "lines"), legend.key.width= unit(1, 'cm')) +
    facet_grid(~ tariff)


showtext_auto()
fn <- "fig10a_cf_emissions.eps"

cairo_ps(file.path(out_pth, fn), width = 8, height = 3, fallback_resolution = 320)
print(p)
dev.off()
showtext_auto(FALSE)
